<?php

namespace app\api\model;

use app\api\service\WxPay;
use think\facade\Log;

class OauthCommission extends \app\common\model\OauthCommission
{
    /**
     * 我的分享（佣金列表）
     * @param $oauthId
     * @param $page
     * @param $size
     * @return array
     * @throws \think\Exception\DbException
     */
    public static function getList($oauthId, $page, $size)
    {
        $offset = ($page - 1) * $size;
        $sql = static::where(['oauth_id' => $oauthId]);

        $model = $sql->field('id, buy_uid AS buyUid, buy_nickname AS buyNickname, 
        order_id AS orderId, commission, create_time AS createTime,
        withdrawal_status AS withdrawalStatus')
            ->order(['create_time' => 'desc'])
            ->limit($offset, $size)
            ->select()
            ->toArray();

        $orderIds = [];
        foreach ($model as $key => $value) {
            $orderIds[$value['orderId']] = $value['orderId'];
        }

        $class = [];
        if ($orderIds) {
            $class = OauthOrderClass::getClassByOrder($orderIds);
        }

        foreach ($model as $key => $value) {
            $model[$key]['className'] = isset($class[$value['orderId']]) ? implode(',', $class[$value['orderId']]) : [];
            $model[$key]['withdrawalStatusText'] = self::attributes('withdrawal_status', $value['withdrawalStatus']);
            $model[$key]['createTime'] = date('Y-m-d H:i:s', $value['createTime']);
        }
        return [
            'sum' => $sql->sum('commission'),
            'text' => Config::getOne('share_text'),
            'list' => $model,
            'count' => $sql->count(),
        ];
    }
}